﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

namespace EffortsTracker
{
    class SessionController
    {
        public Session session;

        public bool running = false;
        public DateTime startDate;
        public DateTime finishDate;
        SessionStatusInterface sessionStatusInterface;
        ScreenCaptureController screenCaptureController;
        Timer timer;
        Random random = new Random();
        KeyTracker keyTracker = new KeyTracker();

        public SessionController(SessionStatusInterface sessionStatusInterface, Project project)
        {
            this.sessionStatusInterface = sessionStatusInterface;
            session = new Session(project);
            screenCaptureController = new ScreenCaptureController(session);
        }

        public SessionController(SessionStatusInterface sessionStatusInterface, Session session)
        {
            this.sessionStatusInterface = sessionStatusInterface;
            this.session = session;
            screenCaptureController = new ScreenCaptureController(session);
        }

        public void start()
        {
            if (session.initialStartDate == DateTime.MinValue)
            {
                session.initialStartDate = DateTime.Now;
                session.finalFinishDate = DateTime.Now;
            }
            startDate = DateTime.Now;
            running = true;
            keyTracker.start();
            session.add();
            timer = new Timer();
            int r = random.Next(30, 600) * 10;
            timer.Interval = r;
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            timer.Start();
        }

        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            screenCaptureController.captureScreen();
            int r = random.Next(30, 600) * 10;
            timer.Interval = r;
        }

        public void stop()
        {
            keyTracker.stop();
            session.sessionStatus = SessionStatus.STOPPED;
            finishDate = DateTime.Now;
            TimeSpan tempTimeSpan = finishDate.Subtract(startDate);
            session.totalTimeSpent += tempTimeSpan.TotalSeconds;
            running = false;
            sessionStatusInterface.updateTimeSpan(session.totalTimeSpent);
            timer.Stop();
        }

        public void stopAndSave()
        {
            if (running)
                stop();
            session.save();
            session.totalTimeSpent = 0;
            session.numKeys = 0;
            session.numMouseClicks = 0;
        }
        public void incrementKeyNum()
        {
            session.numKeys++;
            sessionStatusInterface.updateNumKeys(session.numKeys);
            session.addKeyboardEvent(DateTime.Now);
        }
        public void incrementMouseClicks()
        {
            session.numMouseClicks++;
            sessionStatusInterface.updateNumMouseClicks(session.numMouseClicks);
            session.addMouseClick(DateTime.Now);
        }
    }
}